from pyecharts.charts import Line
from pyecharts import options as opts

# 打开文件取出数据

f = open("2019~2023年全球老龄化数据", "r", encoding="UTF-8")
data_lines = f.readlines()
f.close()  # 关闭文件

# 解析数据
years = []
populations = []
for line in data_lines:
    year, population = line.strip().split(',')
    years.append(year)
    populations.append(float(population))  # 将人口数量转换为整数

# 初始化Line对象
line = Line()

# 添加X轴和Y轴数据
line.add_xaxis(years)

# 添加数据到折线图，并设置线条颜色和粗细
line.add_yaxis("老年人口数量", populations,
               linestyle_opts=opts.LineStyleOpts(color="red", width=5))
# 设置全局配置项，标题等
line.set_global_opts(
    title_opts=opts.TitleOpts(title="2019-2023全球老龄化数据折线图"),
    xaxis_opts=opts.AxisOpts(name="年份"),
    yaxis_opts=opts.AxisOpts(name="老年人口数量（亿）"),
)

# 渲染图表到HTML文件（您也可以在Jupyter Notebook中直接显示）
line.render("aging_population_line_chart.html")